import axios from "axios";
var baseUrl = "http://111.229.255.208:8004/api";

export const myMixin = {
  data() {
    return {
      dataTable: [],
      dataTableOrigin: []
    }
  },
  created() {},
  methods: {
    // 查询
    handleSearch(params, url) {
      axios({
          url: `${baseUrl}/${url}`,
          params,
        })
        .then((res) => {
          if (!res.data.success) {
            this.$message({
              message: res.message,
              type: "error"
            });
          }
          if (res.data.success) {
            console.log(res.data, 1)
            this.dataTable = res.data.data ? res.data.data : []
            this.dataTableOrigin = JSON.parse(JSON.stringify(this.dataTable))
            this.dataTable.forEach(item => {
              if (item.content) {
                item.content = this.removeHTMLTag(item.content)
              }
              if (item.tznr) {
                item.tznr = this.removeHTMLTag(item.tznr)
              }
              if (item.lwjs) {
                item.lwjs = this.removeHTMLTag(item.lwjs)
              }
              if (item.yams) {
                item.yams = this.removeHTMLTag(item.yams)
              }
              if (item.ysjs) {
                item.ysjs = this.removeHTMLTag(item.ysjs)
              }
              if (item.spjs) {
                item.spjs = this.removeHTMLTag(item.spjs)
              }
            });
            this.listQuery.total = res.data.count
          }
        })
    },
    // 查询
    handleMyfcSearch(params, url) {
      axios({
          url: `${baseUrl}/${url}`,
          params,
        })
        .then((res) => {
          let obj = res.data
          let arry = []
          arry[0] = obj
          this.dataTable = arry
          this.dataTableOrigin = JSON.parse(JSON.stringify(this.dataTable))
          this.dataTable.forEach(item => {
            if (item.myjs) {
              item.myjs = this.removeHTMLTag(item.myjs)
            }
            if (item.gzsdesc) {
              item.gzsdesc = this.removeHTMLTag(item.gzsdesc)
            }
            if (item.yams) {
              item.yams = this.removeHTMLTag(item.yams)
            }
          });
        })
    },
    // 添加
    handleAdd(data, url) {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          axios({
              method: "post",
              url: `${baseUrl}/${url}`,
              data,
            })
            .then((res) => {
              if (res.data.success) {
                this.addDialogVisible = false;
                this.search();
                this.$message({
                  message: "添加成功",
                  type: "success"
                });
              } else {
                this.$message({
                  message: res.data.message,
                  type: "error"
                });
              }
            })
        } else {
          this.$message({
            message: "请输入正确的表单信息",
            type: "error"
          });
        }
      });
    },
    // 修改
    handleEdit(data, url) {
      console.log(this.editForm);
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          axios({
              method: "post",
              url: `${baseUrl}/${url}`,
              data,
            })
            .then((res) => {
              if (res.data.success) {
                this.editDialogVisible = false;
                this.search();
                this.$message({
                  message: "修改成功",
                  type: "success"
                });
              } else {
                this.$message({
                  message: res.data.message,
                  type: "error"
                });
              }
            })
        } else {
          this.$message({
            message: "请输入正确的表单信息",
            type: "error"
          });
        }
      });
    },
    // 删除
    handleDelete(data, url) {
      axios({
          method: "post",
          url: `${baseUrl}/${url}`,
          data,
        })
        .then((res) => {
          if (res.data.success) {
            this.search();
            this.$message({
              message: "删除成功",
              type: "success"
            });
          } else {
            this.$message({
              message: res.data.message,
              type: "error"
            });
          }
        })
    },
    removeHTMLTag(str) {
      str = str.replace(/<\/?[^>]*>/g, ''); //去除HTML tag
      str = str.replace(/[ | ]*\n/g, '\n'); //去除行尾空白
      //str = str.replace(/\n[\s| | ]*\r/g,'\n'); //去除多余空行
      str = str.replace(/ /ig, ''); //去掉 
      return str;
    }
  }
}